package org.example.managementsystem.repository;


import org.example.managementsystem.model.Product_Plan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.List;
import java.util.Objects;
@Repository
public class Product_PlanDao {
    private final JdbcTemplate jdbcTemplate;
    @Autowired
    public Product_PlanDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    //添加计划
    public Long insertProduct_Plan(Product_Plan product_plan) {
        String sql = "INSERT INTO production_plan ( start_time, end_time, description) " +
                "VALUES (?, ?, ?)";

        KeyHolder keyHolder = new GeneratedKeyHolder();
        jdbcTemplate.update(connection -> {
            PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);

            ps.setDate(1, product_plan.getStart_time() != null ?
                    java.sql.Date.valueOf(product_plan.getStart_time()) : null);
            ps.setDate(2, product_plan.getEnd_time() != null ?
                    java.sql.Date.valueOf(product_plan.getEnd_time()) : null);
            ps.setString(3, product_plan.getDescription());
            return ps;
        }, keyHolder);
        return Objects.requireNonNull(keyHolder.getKey()).longValue();
    }
    public int updateProduct_Plan(Product_Plan product_plan) {
        String sql = "UPDATE production_plan SET start_time = ?, end_time = ?, description = ? " +
                "WHERE plan_id = ?";

        return jdbcTemplate.update(sql,

                product_plan.getStart_time() != null ? java.sql.Date.valueOf(product_plan.getStart_time()) : null,
                product_plan.getEnd_time() != null ? java.sql.Date.valueOf(product_plan.getEnd_time()) : null,
                product_plan.getDescription(),
                product_plan.getPlan_id());
    }
    public Product_Plan getProduct_PlanById(Long plan_id) {
        String sql = "SELECT * FROM production_plan WHERE plan_id = ?";
        List<Product_Plan> product_plans = jdbcTemplate.query(sql, new Product_PlanRowMapper(), plan_id);
        return product_plans.isEmpty() ? null : product_plans.get(0);
    }
    public int deleteProduct_Plan(Long plan_id) {
        String sql = "DELETE FROM production_plan WHERE plan_id = ?";
        return jdbcTemplate.update(sql, plan_id);
    }

    public List<Product_Plan> getAllProduct_Plans() {
        String sql = "SELECT * FROM production_plan";
        return jdbcTemplate.query(sql, new Product_PlanRowMapper());
    }
    private static class Product_PlanRowMapper implements RowMapper<Product_Plan> {
        @Override
        public Product_Plan mapRow(ResultSet rs, int rowNum) throws SQLException {
            Product_Plan product_plan = new Product_Plan();
            product_plan.setPlan_id(rs.getLong("plan_id"));
            Date start_time =  rs.getDate("start_time");
            if(start_time!=null){
                product_plan.setStart_time(((java.sql.Date) start_time).toLocalDate());
            }
            Date end_time =  rs.getDate("end_time");
            if(start_time!=null){
                product_plan.setEnd_time(((java.sql.Date) end_time).toLocalDate());
            }
            product_plan.setDescription(rs.getString("description"));

            return product_plan;
        }
    }
}
